*************************************************************************
*************************************************************************
*							Elusive indeed - 							*
*		the mechanical versus psychological effects of electoral		*
*						rules at the district level						*
*************************************************************************
*	Purpose:		Do-file to replicate all figures 					*
*	File Name:		do_harfst+et+al_figures.do							*
*	Input File:		data_harfst+et+al.dta								*
*************************************************************************
*************************************************************************


***Figure 1: Marginal effect of cleavages on Hopeless Votes, ENLOP, CP, 
*			 and ENEP at different values of district maginiutuede

**Hopeless Votes

use "XXX\data_harfst+et+al", clear

xtmixed hopeless party_count c.d_seat##c.cleavage if num_reg==1 || iso:, covariance(independent) || time:, covariance(independent) mle
	predict yhat_margin
	
	summ d_seat
	margins, dydx(cleavage) at(d_seat=(0(0.25)4.5)) saving(margin_plot_hopeless, replace)

*Margins		
use margin_plot_hopeless, clear
	rename _at1 hopeless_margin
	rename _at2 d_seat_margin
	rename _at3 cleavage_margin
	rename _at num

	gen d_seat_nolog_margin = exp(d_seat_margin)
	
*Graph
twoway ///
	(line _margin d_seat_nolog_margin, lpattern(solid) lcolor(black)) ///
	(line _ci_lb d_seat_nolog_margin, lpattern(dash) lcolor(black)) ///
	(line _ci_ub d_seat_nolog_margin, lpattern(dash) lcolor(black)), ///
	xtitle("District Magnitude (unlogged)")	///
	ytitle("Marginal Effect of Cleavages on Hopeless Votes") ///
		yline(0) ///
	legend(off) saving(nolog_margin_hopeless, replace)


**ENOLP
	
use "XXX\data_harfst+et+al", clear
	
xtmixed enlop_o party_count c.d_seat##c.cleavage if num_reg==1 || iso:, covariance(independent) || time:, covariance(independent) mle
	predict yhat_margin
	
	summ d_seat
	margins, dydx(cleavage) at(d_seat=(0(0.25)4.5)) saving(margin_plot_enlop, replace)

***Margins		
use margin_plot_enlop, clear
	rename _at1 enlop_o_margin
	rename _at2 d_seat_margin
	rename _at3 cleavage_margin
	rename _at num

	gen d_seat_nolog_margin = exp(d_seat_margin)
	
***Graph
twoway ///
	(line _margin d_seat_nolog_margin, lpattern(solid) lcolor(black)) ///
	(line _ci_lb d_seat_nolog_margin, lpattern(dash) lcolor(black)) ///
	(line _ci_ub d_seat_nolog_margin, lpattern(dash) lcolor(black)), ///
	xtitle("District Magnitude (unlogged)")	///
	ytitle("Marginal Effect of Cleavages on ENLOP") ///
		yline(0) ///
	legend(off) saving(nolog_margin_enlop, replace)
	

**Coordination Product

use "XXX\data_harfst+et+al", clear

xtmixed coordination party_count c.d_seat##c.cleavage if num_reg==1 || iso:, covariance(independent) || time:, covariance(independent) mle
	predict yhat_margin
	
	summ d_seat
	margins, dydx(cleavage) at(d_seat=(0(0.25)4.5)) saving(margin_plot_coordination, replace)

***Margins		
use margin_plot_coordination, clear
	rename _at1 coordination_margin
	rename _at2 d_seat_margin
	rename _at3 cleavage_margin
	rename _at num

	gen d_seat_nolog_margin = exp(d_seat_margin)
	
***Graph
twoway ///
	(line _margin d_seat_nolog_margin, lpattern(solid) lcolor(black)) ///
	(line _ci_lb d_seat_nolog_margin, lpattern(dash) lcolor(black)) ///
	(line _ci_ub d_seat_nolog_margin, lpattern(dash) lcolor(black)), ///
	xtitle("District Magnitude (unlogged)")	///
	ytitle("Marginal Effect of Cleavages on Coordination Product") ///
		yline(0) ///
	legend(off) saving(nolog_margin_coordination, replace)


**ENEP

use "XXX\data_harfst+et+al", clear

xtmixed enep_cst_o party_count c.d_seat##c.cleavage if num_reg==1 || iso:, covariance(independent) || time:, covariance(independent) mle
	predict yhat_margin
	
	summ d_seat
	margins, dydx(cleavage) at(d_seat=(0(0.25)4.5)) saving(margin_plot_enep, replace)

***Margins		
use margin_plot_enep, clear
	rename _at1 enep_margin
	rename _at2 d_seat_margin
	rename _at3 cleavage_margin
	rename _at num

	gen d_seat_nolog_margin = exp(d_seat_margin)
	
***Graph
twoway ///
	(line _margin d_seat_nolog_margin, lpattern(solid) lcolor(black)) ///
	(line _ci_lb d_seat_nolog_margin, lpattern(dash) lcolor(black)) ///
	(line _ci_ub d_seat_nolog_margin, lpattern(dash) lcolor(black)), ///
	xtitle("District Magnitude (unlogged)")	///
	ytitle("Marginal Effect of Cleavages on ENEP") ///
		yline(0) ///
	legend(off) saving(nolog_margin_enep, replace)


**Combine Graphs
	
graph combine nolog_margin_enep.gph
			  nolog_margin_hopeless.gph ///
			  nolog_margin_enlop.gph ///
			  nolog_margin_coordination.gph ///
			   
	
		
***Figure A1: Replication of Figure 1 from main text, excluding Azores
*			  and Madeira (Table A2, Model A2)

*use "XXX\data_harfst+et+al", clear
use "C:\Users\kosankes\Google Drive\Electoralsystemchange\paper ideas and work in progress\Elusive Indeed\2nd R&R\data\data_harfst+et+al", clear
	
xtmixed enep_cst_o party_count c.d_seat##c.cleavage if unique_id!=37 & unique_id!=38 & num_reg==1  || iso:, covariance(independent) || time:, covariance(independent) mle
	predict yhat_margin
	
	summ d_seat
	margins, dydx(cleavage) at(d_seat=(0(0.25)4.5)) saving(margin_plot_noaandm, replace)

***Margins		
use margin_plot_noaandm, clear
	rename _at1 enep_margin
	rename _at2 d_seat_margin
	rename _at3 cleavage_margin
	rename _at num

	gen d_seat_nolog_margin = exp(d_seat_margin)
	
***Graph
twoway ///
	(line _margin d_seat_nolog_margin, lpattern(solid) lcolor(black)) ///
	(line _ci_lb d_seat_nolog_margin, lpattern(dash) lcolor(black)) ///
	(line _ci_ub d_seat_nolog_margin, lpattern(dash) lcolor(black)), ///
	xtitle("District Magnitude (unlogged)")	///
	ytitle("Marginal Effect of Cleavages on ENEP") ///
		yline(0) ///
	legend(off) saving(nolog_margin_enep_noaandm, replace)

	
***Figure A2: Marginal Effects of three partial Cleavage Indicators
*			  at different levels of district magnitude (Table A3, Model A5)

**Land-Industry

*use "XXX\data_harfst+et+al", clear
use "C:\Users\kosankes\Google Drive\Electoralsystemchange\paper ideas and work in progress\Elusive Indeed\2nd R&R\data\data_harfst+et+al", clear
	
xtmixed enep_cst_o party_count c.d_seat##c.(het_ur_agrar_mean het_sc_reli_mean het_ow_worker_mean) if num_reg==1 || iso:, covariance(independent) || time:, covariance(independent) mle
	predict yhat_margin
	
	summ d_seat
	margins, dydx(het_ur_agrar_mean) at(d_seat=(0(0.25)4.5)) saving(margin_plot_agrar, replace)

***Margins		
use margin_plot_agrar, clear
	rename _at1 enep_margin
	rename _at2 d_seat_margin
	rename _at3 agrar_margin
	rename _at4 reli_magrin
	rename _at5 work_margin
	rename _at num

	gen d_seat_nolog_margin = exp(d_seat_margin)
	
***Graph
twoway ///
	(line _margin d_seat_nolog_margin, lpattern(solid) lcolor(black)) ///
	(line _ci_lb d_seat_nolog_margin, lpattern(dash) lcolor(black)) ///
	(line _ci_ub d_seat_nolog_margin, lpattern(dash) lcolor(black)), ///
	xtitle("District Magnitude (unlogged)")	///
	ytitle("Marginal Effect: Land-Industry") ///
		yline(0) ///
	legend(off) saving(nolog_agrar, replace)


**State-Church

*use "XXX\data_harfst+et+al", clear
use "C:\Users\kosankes\Google Drive\Electoralsystemchange\paper ideas and work in progress\Elusive Indeed\2nd R&R\data\data_harfst+et+al", clear

xtmixed enep_cst_o party_count c.d_seat##c.(het_ur_agrar_mean het_sc_reli_mean het_ow_worker_mean) if num_reg==1 || iso:, covariance(independent) || time:, covariance(independent) mle
	predict yhat_margin
	
	summ d_seat
	margins, dydx(het_sc_reli_mean) at(d_seat=(0(0.25)4.5)) saving(margin_plot_reli, replace)

***Margins		
use margin_plot_reli, clear
	rename _at1 enep_margin
	rename _at2 d_seat_margin
	rename _at3 agrar_margin
	rename _at4 reli_magrin
	rename _at5 worker_margin
	rename _at num

	gen d_seat_nolog_margin = exp(d_seat_margin)
	
***Grafik
twoway ///
	(line _margin d_seat_nolog_margin, lpattern(solid) lcolor(black)) ///
	(line _ci_lb d_seat_nolog_margin, lpattern(dash) lcolor(black)) ///
	(line _ci_ub d_seat_nolog_margin, lpattern(dash) lcolor(black)), ///
	xtitle("District Magnitude (unlogged)")	///
	ytitle("Marginal Effect: State-Church") ///
		yline(0) ///
	legend(off)  saving(nolog_reli, replace)

		
**Owner-Worker	

*use "XXX\data_harfst+et+al", clear
use "C:\Users\kosankes\Google Drive\Electoralsystemchange\paper ideas and work in progress\Elusive Indeed\2nd R&R\data\data_harfst+et+al", clear

xtmixed enep_cst_o party_count c.d_seat##c.(het_ur_agrar_mean het_sc_reli_mean het_ow_worker_mean) if num_reg==1 || iso:, covariance(independent) || time:, covariance(independent) mle
	predict yhat_margin
	
	summ d_seat
	margins, dydx(het_ow_worker_mean) at(d_seat=(0(0.25)4.5)) saving(margin_plot_work, replace)

***Margins		
use margin_plot_work, clear
	rename _at1 enep_margin
	rename _at2 d_seat_margin
	rename _at3 agrar_margin
	rename _at4 reli_magrin
	rename _at5 worker_margin
	rename _at num

	gen d_seat_nolog_margin = exp(d_seat_margin)
	
***Graph
twoway ///
	(line _margin d_seat_nolog_margin, lpattern(solid) lcolor(black)) ///
	(line _ci_lb d_seat_nolog_margin, lpattern(dash) lcolor(black)) ///
	(line _ci_ub d_seat_nolog_margin, lpattern(dash) lcolor(black)), ///
	xtitle("District Magnitude (unlogged)")	///
	ytitle("Marginal Effect: Owner-Worker") ///
		yline(0) ///
	legend(off) saving(nolog_work, replace) 


**Combine Graphs
	
graph combine nolog_agrar.gph ///
			  nolog_reli.gph ///
			  nolog_work.gph 























	





